package com.ossjk.qlh.specializedSubject.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.PageUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ossjk.core.base.controller.BaseController;
import com.ossjk.core.vo.ResponseBean;
import com.ossjk.qlh.specializedSubject.entity.Subjecttypes;
import com.ossjk.qlh.specializedSubject.service.ISubjecttypesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Copyright  2023-03-06 QLH. Tech Ltd. All rights reserved.
 * 
 * @Package: com.ossjk.qlh.specializedSubject.controller
 * @ClassName: SubjecttypesController
 * @Description: Subjecttypes-控制器
 * @author: Levi
 * @date:  2023-03-06 15:46:51 
 */
@Api(tags = "专业类别表,维护一个4级树，类别[高职] - [大类名称] - [二级类名称] - [专业名称]")
@RestController
@RequestMapping("/specializedSubject/subjecttypes")
public class SubjecttypesController extends BaseController {
	
	@Autowired
	private ISubjecttypesService iSubjecttypesService;

	@ApiOperation(value = "列表")
	@RequiresPermissions("00050000")
	@GetMapping("/list")
	public ResponseBean list( ) {
		QueryWrapper<Subjecttypes> queryWrapper = new QueryWrapper<Subjecttypes>();

		List<Subjecttypes> list =iSubjecttypesService.list(queryWrapper);
		List<Subjecttypes> list1 = list.stream().sorted(Comparator.comparing(a -> a.getIdex())).collect(Collectors.toList());
		List<Subjecttypes> list2 = list1.stream().sorted(Comparator.comparing(a -> a.getLevel())).collect(Collectors.toList());
		return ResponseBean.create(1000,"操作成功",list2);
	}

	@ApiOperation(value = "添加")
	@RequiresPermissions("000500000005")
	@PostMapping("/save")
	public ResponseBean save(@RequestBody Subjecttypes record) {
		if (iSubjecttypesService.save(record)) {
			return ResponseBean.Success();
		} else {
			return ResponseBean.Fail();
		}
	}

	@ApiOperation(value = "去编辑")
	@RequiresPermissions("000500001010")
	@GetMapping("/toUpdate")
	public ResponseBean<Subjecttypes> toUpdate(@ApiParam(value = "id") @RequestParam(name = "id") String id) {
		Subjecttypes subjecttypes = iSubjecttypesService.getById(id);
		if (ObjectUtil.isNotNull(subjecttypes)) {
			return ResponseBean.Success(subjecttypes);
		} else {
			return ResponseBean.Fail();
		}
	}


	@ApiOperation(value = "编辑")
	@RequiresPermissions("000500000005")
	@PutMapping("/update")
	public ResponseBean update(@RequestBody Subjecttypes record) {
		if (iSubjecttypesService.updateById(record)) {
			return ResponseBean.Success();
		} else {
			return ResponseBean.Fail();
		}
	}

	@ApiOperation(value = "删除")
	@RequiresPermissions("000500000010")
	@DeleteMapping("/remove")
	public ResponseBean remove(@ApiParam(value = "ids") @RequestParam(name = "ids") String[] ids) {
		if (iSubjecttypesService.removeByIds(Arrays.asList(ids))) {
			return ResponseBean.Success();
		} else {
			return ResponseBean.Fail();
		}
	}
}
